package com.ruoyi.io;
import java.io.*;
public class ByteArrayOutputStreamExample {
    public static void main(String[] args) {
        // 创建字节数组输出流对象
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            // 将字符串转换为字节数组，并写入到字节数组输出流中
            baos.write("Hello, World!".getBytes("UTF-8"));
            // 将字节数组输出流中的数据转换为字节数组
            byte[] bytes = baos.toByteArray();
            // 将字节数组转换为字符串，并输出到控制台
            String content = new String(bytes, "UTF-8");
            System.out.println(content);
        } catch (IOException e) {
            System.out.println("Error writing to byte array: " + e.getMessage());
        } finally {
            // 关闭字节数组输出流
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing byte array output stream: " + e.getMessage());
            }
        }
    }
}
